package ncku.account.event;

import java.io.*;
import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

import ncku.account.data.Category;
import ncku.account.data.GlobalData;
import ncku.account.data.SubTransaction;
import ncku.account.data.Transaction;

import jxl.*;
import jxl.read.biff.*;
import jxl.write.Label;

/*
 由使用者輸入帳戶名稱，然後會去開啟指定的excel 檔案，
 並且逐筆讀取裡面的資料進來程式
 * */

public class Import {
	//帳戶名稱
	private String name;
	Scanner scanner = new Scanner(System.in);
	//交易資料陣列
	private static ArrayList<Transaction> transactions;
	private static SimpleDateFormat df = new SimpleDateFormat();
	private static ArrayList<Category> categories;
	//子細項
	private ArrayList<SubTransaction> subTransactions;  
	//交易日期
	private Date date;
	//交易類別在
	private Category category;
	//子細項是否讀完flag
	private boolean subEnd;
	//帳戶類別數目
	private int cateGoryNumber;
	//選擇要匯入的帳戶名稱
	public Import(String name){
		this.name  = name;
		//創一個新的交易list 準備匯入
		transactions = new ArrayList<Transaction>();
		//創一個新的類別list 準備匯
		categories = new ArrayList<Category>();
		df.applyPattern("yyyy/MM/dd");
		subEnd = false;
	}
	
	//讀取帳戶名稱的Excel 檔
	public String Start() {
		try{
			//將程式的帳戶名稱改成匯入的
			GlobalData.accountName = name;
			//讀取帳戶的Excel 檔
			Workbook workbook = Workbook.getWorkbook(new File(name+".xls"));	
			Sheet sheet = workbook.getSheet("My Sheet");
			//讀取帳戶金錢
			GlobalData.accountMoney = Integer.valueOf(sheet.getCell(1, 0).getContents());
			//將現在餘額讀入
			GlobalData.balanceMoney = Integer.valueOf(sheet.getCell(1, 0).getContents());
			//用迴圈讀取Excel 檔案裡面的交易記錄，日期，類別，金額都讀進來新的transactions
			//sheet.getRows()-2 總筆數扣掉前面兩筆不用讀
			 for(int i=2; i <= sheet.getRows()-1;i++){    	   
			
				try {
					//讀取日期
					date = df.parse(sheet.getCell(1, i).getContents());
					//讀取類別
					category = new Category(sheet.getCell(2, i).getContents());
					subTransactions = new ArrayList<SubTransaction>();
					
				//如果那格沒有日期代表是子細項的部分，會拋出exception ，跳進來處理子細項
				} catch (ParseException e) {
					try{
						while(sheet.getCell(1, i).getContents().length() ==0){
						//沒讀到日期，代表那格是空白，進入子細項部份
						SubTransaction subTrans = new SubTransaction(sheet.getCell(3, i).getContents(),Integer.valueOf(sheet.getCell(4, i).getContents()));
						subTransactions.add(subTrans);
						i++;
						}
					i--;
					//讀到excel 檔案尾時會超出陣列索引，此時往回退以免錯誤
					} catch(ArrayIndexOutOfBoundsException ae){
						i--;
					}
					
					
				}
				//不做任何事
				if(sheet.getCell(0, i).getContents().equals("#")){
				}
					
				else{
					//新增交易資料並加入資料陣列，description 跟amount 在這裡讀取
					Transaction trans = new Transaction(date, sheet.getCell(3, i).getContents(), Integer.valueOf(sheet.getCell(4, i).getContents()),  category,subTransactions);
					transactions.add(trans);
				}
					
		    } 
			//讀取匯入的帳戶原有類別總數目
			cateGoryNumber = Integer.valueOf(sheet.getCell(6, 0).getContents());
			//依序讀入陣列
			for(int i =0;i < cateGoryNumber;i++){
				Category ca = new Category(sheet.getCell(6, i+1).getContents());
				categories.add(ca);
			}
			workbook.close();  
			//最後讀完時將程式舊的覆蓋過去
			GlobalData.loadAccountImformationTransaction(transactions);
			GlobalData.loadAccountImformationCategory(categories);
		} catch (BiffException e) {
			return "readError";
		} catch (IOException e) {
			return "IOError";
		}
		return "success";
	}

}